package cn.haut.online_read.controller;

import cn.haut.online_read.enrity.BookInfo;
import cn.haut.online_read.service.BookshelfService;
import cn.haut.online_read.utils.ResponseUtils;
import cn.haut.online_read.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.haut.online_read.utils.Constant.TOKEN_KEY;
import static cn.haut.online_read.utils.ResponseUtils.*;

/**
 * @author xing
 * @date 2021-05-14 18:52
 */
@Controller("bookshelfController")
@RequestMapping(value = "/bookshelf")
@Slf4j
public class BookshelfController {

    private BookshelfService bookshelfService;
    private TokenUtils tokenUtils;

    @Autowired
    public void setBookshelfService(BookshelfService bookshelfService){
        this.bookshelfService = bookshelfService;
    }

    @Autowired
    public void setTokenUtils(TokenUtils tokenUtils) {
        this.tokenUtils = tokenUtils;
    }

    /**
     * 添加到书架
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public String addShelf(
            @RequestBody Map<String, String> map,
            @RequestHeader(TOKEN_KEY) String token
    ) {
        String username = map.get("username");
        String id = map.get("id");
        String name = map.get("name");
        String imgUrl = map.get("imgUrl");
        // token 验证不通过
        if(token == null || !tokenUtils.validateToken(username, token)) {
            // return
            return createTokenError();
        }
        boolean addBoolean = bookshelfService.addBookshelfByUserId(username, new BookInfo(id, name, null, null, imgUrl, null, null));
        if(addBoolean) {
            return ResponseUtils.createSuccess("");
        }
        return ResponseUtils.createError("添加失败");
    }

    /**
     * 判断是否存在书架里
     *
     * @param requestMap
     * @return
     */
    @PostMapping(value = "/exist")
    @ResponseBody
    public String existShelf(
            @RequestBody Map<String, String> requestMap,
            @RequestHeader(TOKEN_KEY) String token

    ) {
        String username = requestMap.get("username");
        String bookId = requestMap.get("bookId");
        // token 验证不通过
        if(token == null || !tokenUtils.validateToken(username, token)) {
            // return
            return createTokenError();
        }
        boolean exist = bookshelfService.isExist(username, bookId);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("exist", exist);
        return ResponseUtils.addSuccess(responseMap);
    }

    /**
     * 获取书架所有书籍
     *
     * @param requestMap
     * @return
     */
    @PostMapping(value = "/getBooks")
    @ResponseBody
    public String getBooks(
            @RequestBody Map<String, String> requestMap,
            @RequestHeader(TOKEN_KEY) String token
    ) {
        if(token != null){
            log.error("exist token:{}", token);
        }
        String username = requestMap.get("username");
        // token 验证不通过
        if(token == null || !tokenUtils.validateToken(username, token)) {
            // return
            return createTokenError();
        }
        List<BookInfo> bookInfoList = bookshelfService.getBooks(username);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("books", bookInfoList);
        return addSuccess(responseMap);
    }

    /**
     * 从书架移除书籍
     * @param requestMap
     * @return
     */
    @PostMapping(value = "/remove")
    @ResponseBody
    public String removeBook(
            @RequestBody Map<String, String> requestMap,
            @RequestHeader(TOKEN_KEY) String token
    ) {
        String username = requestMap.get("username");
        String bookId = requestMap.get("bookId");
        // token 验证不通过
        if(token == null || !tokenUtils.validateToken(username, token)) {
            // return
            return createTokenError();
        }
        boolean remove = bookshelfService.removeBook(username, bookId);
        if(remove) {
            return createSuccess();
        } else {
            return createError("移除失败");
        }
    }
}
